package cn.stylefeng.guns.modular.person.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.person.entity.Person;
import cn.stylefeng.guns.modular.person.mapper.PersonMapper;
import  cn.stylefeng.guns.modular.person.service.PersonService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 人员表 服务实现类
 * </p>
 *
 * @author guohognzhi
 * @since 2021-04-13
 */
@Service
public class PersonServiceImpl extends ServiceImpl<PersonMapper, Person> implements PersonService {

    @Override
    public void add(Person param){
        Person entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(Person param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(Person param){
        Person oldEntity = getOldEntity(param);
        Person newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public Person findBySpec(Person param){
        return null;
    }

    @Override
    public List<Person> findListBySpec(Person param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(Person param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(Person param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Person getOldEntity(Person param) {
        return this.getById(getKey(param));
    }

    private Person getEntity(Person param) {
        Person entity = new Person();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
